package com.example.clientsideapplication;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class GpsTransmissionService extends Service {
	private Thread gpsTransmissionService;
	private Context context;
	private String serverAddress;
	private int serverSocket;
	private double latitude, longitude;
	private LocationManager locationManager;
	private boolean gpsIsEnabled;
	private int meters;
	private final static String MY_PREFERENCES = "myAppPref";
	private Handler locationHandler;
	private MyLocationListener locationListener;

	@Override
	  public int onStartCommand(Intent intent, int flags, int startId) {
		serverAddress = intent.getStringExtra("serverAddress");
		serverSocket = intent.getIntExtra("serverSocket", 21111);
		context = getApplicationContext();
		
		SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
		meters =  prefs.getInt("meters", 0);
		
		gpsTransmissionService = new Thread(new Runnable() {
			public void run() {
				try {
					Looper.prepare();
					locationHandler = new Handler();
					
					locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
					locationListener = new MyLocationListener();
					
					gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
					
					if(gpsIsEnabled){
						locationManager.requestLocationUpdates(
								LocationManager.GPS_PROVIDER, 0, Math.round(meters*0.1),
								locationListener);
					}
					else{
						locationManager.requestLocationUpdates(
								LocationManager.NETWORK_PROVIDER, 0, Math.round(meters*0.1),
								locationListener);
					}
					
					Looper.loop();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, "Gps Transmission Thread");
		
		gpsTransmissionService.start();
	    return Service.START_NOT_STICKY;
	  }

	  @Override
	  public IBinder onBind(Intent intent) {
	    return null;
	  }
	  
	  public void onDestroy(){
		  locationManager.removeUpdates(locationListener);
		  locationHandler.getLooper().quit();
		  
		  stopSelf();
	  }
	  
	  private class MyLocationListener implements LocationListener {

			@Override
			public void onLocationChanged(Location location) {
				latitude = location.getLatitude();
				longitude = location.getLongitude();
				
				Intent intent = new Intent(context, TrasmissionService.class);
				intent.putExtra("serverAddress", serverAddress);
				intent.putExtra("serverSocket", serverSocket);
				intent.putExtra("latitude", latitude);
				intent.putExtra("longitude", longitude);
				startService(intent);
			}

			@Override
			public void onProviderDisabled(String provider) {

			}

			@Override
			public void onProviderEnabled(String provider) {

			}

			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {

			}
		}

}
